﻿using System;
using System.Collections;
using System.IO;
using System.Windows.Forms;
using HiStaff.Util;
using HiStaff.Util.Enum;
using HiStaff.Control;
using Janus.Windows.EditControls;
using Janus.Windows.GridEX;
using Janus.Windows.GridEX.Export;
using Janus.Windows.UI.CommandBars;
using Janus.Windows.UI.Dock;
using System.Drawing;
using System.ComponentModel;
using System.Reflection;
using System.Collections.Generic;
using HiStaff.Domain.Sys;
using System.Drawing.Text;
using System.Runtime.InteropServices;
using HiStaff.iSystem;

namespace HiStaff.Base
{
    public partial class FormBase : Form
    {

        public FormBase()
        {
            this.Font = SystemFont.FontSegoeUi;
            InitializeComponent();
        }
        #region Private Variables

        private const string BAR_PROGRESSBAR_KEY = "PROGRESSBAR";
        private const string BAR_STATUSTEXT_KEY = "STATUSTEXT";

        private const string MSG_BEFORE_CLOSE = "Bạn đang chỉnh sửa dữ liệu.\nBạn muốn lưu dữ liệu trước khi thoát?";
        private const string MSG_BREAKBGW_TOCLOSE = "Hệ thống đang tiến hành xử lý dữ liệu, nếu đóng màn hình lệnh xử lý dữ liệu sẽ bị huỷ.\nBạn có thực sự muốn đóng?";

        private const string CTX_EXPEXCELKEY = "CONTEXTEXPORTEXCEL";
        private static List<System.Windows.Forms.Control> lstCtrlDataField;
        private bool confirmWhenDelete = true;
        #endregion

        #region Public Properties & Variables
        protected static List<System.Windows.Forms.Control> ListCtrlDataField
        {
            get { return lstCtrlDataField; }
            set
            {
                lstCtrlDataField = value;
            }
        }
        [DefaultValue(true)]
        public bool ConfirmWhenDelete { get { return confirmWhenDelete; } set { confirmWhenDelete = value; } }
        protected bool SetEnabledCommandNew
        {
            get
            {
                return GetEnableCommand(CommandBarManager.ActionCommand.New);
            }
            set
            {
                SetEnableCommand(CommandBarManager.ActionCommand.New, value);
            }
        }
        protected bool SetEnabledCommandEdit
        {
            get
            {
                return GetEnableCommand(CommandBarManager.ActionCommand.Edit);
            }
            set
            {
                SetEnableCommand(CommandBarManager.ActionCommand.Edit, value);
            }
        }
        protected bool SetEnabledCommandDelete
        {
            get
            {
                return GetEnableCommand(CommandBarManager.ActionCommand.Delete);
            }
            set
            {
                SetEnableCommand(CommandBarManager.ActionCommand.Delete, value);
            }
        }
        protected bool SetEnabledCommandSave
        {
            get
            {
                return GetEnableCommand(CommandBarManager.ActionCommand.Save);
            }
            set
            {
                SetEnableCommand(CommandBarManager.ActionCommand.Save, value);
            }
        }
        protected bool SetEnabledCommandCancel
        {
            get
            {
                return GetEnableCommand(CommandBarManager.ActionCommand.Cancel);
            }
            set
            {
                SetEnableCommand(CommandBarManager.ActionCommand.Cancel, value);
            }
        }
        protected bool SetEnabledCommandApproveFlexible
        {
            get
            {
                return GetEnableCommand(CommandBarManager.ActionCommand.ApproveFlexible);
            }
            set
            {
                SetEnableCommand(CommandBarManager.ActionCommand.ApproveFlexible, value);
            }
        }

        public int ProgressBarWidth
        {
            get
            {
                return barMainBase.Panels[BAR_PROGRESSBAR_KEY].Width;
            }
            set
            {
                barMainBase.Panels[BAR_PROGRESSBAR_KEY].Width = value;
            }
        }

        public bool VisibleProgressBar
        {
            get
            {
                return barMainBase.Panels[BAR_PROGRESSBAR_KEY].Visible;
            }
            set
            {
                barMainBase.Panels[BAR_PROGRESSBAR_KEY].Visible = value;
            }
        }

        public bool VisibleStatusTextBar
        {
            get
            {
                return barMainBase.Panels[BAR_STATUSTEXT_KEY].Visible;
            }
            set
            {
                barMainBase.Panels[BAR_STATUSTEXT_KEY].Visible = value;
            }
        }

        public bool VisibleStatusBar
        {
            get
            {
                return barMainBase.Visible;
            }
            set
            {
                barMainBase.Visible = value;
            }
        }

        public int ReportProgressValue
        {
            get;
            set;
        }

        public string ReportProgressText
        {
            get;
            set;
        }

        protected bool IsProcessing = false;
        protected bool IsLoaded = false;

        public System.Windows.Forms.Control AddForcusControl { get; set; }
        public System.Windows.Forms.Control EditForcusControl { get; set; }
        public System.Windows.Forms.Control SaveForcusControl { get; set; }
        public System.Windows.Forms.Control CancelForcusControl { get; set; }
        public System.Windows.Forms.Control DeleteForcusControl { get; set; }

        public FormStatus BaseStatus { get; set; }
        #endregion
        #region Private Event & Function
        private void FormBase_Load(object sender, EventArgs e)
        {
            try
            {
                if (IsProcessing)
                    return;
                else
                    IsProcessing = true;
                this.Cursor = Cursors.WaitCursor;
                IsLoaded = FormLoad();
                this.Cursor = Cursors.Default;
                IsProcessing = false;
            }
            catch (Exception ex)
            {
                IsProcessing = false;
                this.Cursor = Cursors.Default;
                //grbMain.Enabled = true;

                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Log.Instance.WriteExceptionLog(ex, this.Name + "[" + this.Text + "]");
            }
        }
        private void uiCommandBar1_CommandClick(object sender, CommandEventArgs e)
        {
            bool result = false;
            try
            {
                if (IsProcessing)
                {
                    return;
                }
                else
                {
                    IsProcessing = true;
                    this.Cursor = Cursors.WaitCursor;
                    //grbMain.Enabled = false;
                }

                switch (ConvertHelper.ConverStringToEnum<CommandBarManager.ActionCommand>(e.Command.Key))
                {
                    case CommandBarManager.ActionCommand.New:
                        result = AddNewButtonClick();
                        if (result)
                        {
                            BaseStatus = FormStatus.Add;

                            ResetInputControl(this);
                            SetStatusInputControl(this);
                            SetToolStripButtonBar(CommandBarManager.ActionCommand.New);

                            if (AddForcusControl != null)
                            {
                                this.AddForcusControl.Focus();
                            }
                        }
                        break;

                    case CommandBarManager.ActionCommand.Edit:
                        result = EditButtonClick();
                        if (result)
                        {
                            BaseStatus = FormStatus.Edit;

                            SetStatusInputControl(this);
                            SetToolStripButtonBar(CommandBarManager.ActionCommand.Edit);

                            if (EditForcusControl != null)
                            {
                                this.EditForcusControl.Focus();
                            }
                        }
                        break;

                    case CommandBarManager.ActionCommand.Save:
                        if (CheckValidation(this))
                        {
                            result = SaveButtonClick();
                            if (result)
                            {
                                BaseStatus = FormStatus.View;

                                SetStatusInputControl(this);
                                SetToolStripButtonBar(CommandBarManager.ActionCommand.Save);

                                if (SaveForcusControl != null)
                                {
                                    this.SaveForcusControl.Focus();
                                }
                            }
                        }
                        break;

                    case CommandBarManager.ActionCommand.Cancel:
                        result = CancelButtonClick();
                        if (result)
                        {
                            BaseStatus = FormStatus.View;

                            SetStatusInputControl(this);
                            SetToolStripButtonBar(CommandBarManager.ActionCommand.Cancel);

                            if (CancelForcusControl != null)
                            {
                                this.CancelForcusControl.Focus();
                            }
                        }
                        break;

                    case CommandBarManager.ActionCommand.Delete:
                        bool confirm = true;
                        if (ConfirmWhenDelete)
                        {
                            if (MessageBox.Show(GetMessageById("MSG_CONFIRM_DELETE"), this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                            {
                                confirm = false;
                            }
                        }
                        if (confirm)
                        {
                            result = DeleteButtonClick();
                            if (result)
                            {
                                BaseStatus = FormStatus.View;

                                SetStatusInputControl(this);
                                SetToolStripButtonBar(CommandBarManager.ActionCommand.Delete);

                                if (DeleteForcusControl != null)
                                {
                                    this.DeleteForcusControl.Focus();
                                }
                            }
                        }
                        break;

                    case CommandBarManager.ActionCommand.Exit:

                        if (BaseStatus == FormStatus.Edit || BaseStatus == FormStatus.Add)
                        {
                            System.Windows.Forms.DialogResult dlgReult = MessageBox.Show(GetMessageById("MSG_BEFORE_CLOSE"), this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
                            if (dlgReult == System.Windows.Forms.DialogResult.No)
                            {
                                this.Close();
                                this.Dispose();
                            }
                            else if (dlgReult == System.Windows.Forms.DialogResult.Yes)
                            {
                                result = CancelButtonClick();
                                if (result)
                                {
                                    this.Close();
                                    this.Dispose();
                                }
                            }
                        }
                        else
                        {
                            this.Close();
                            this.Dispose();
                        }
                        break;

                    case CommandBarManager.ActionCommand.Validate:
                        result = ValidateButtonClick();
                        if (result)
                        {
                            BaseStatus = FormStatus.View;
                            SetToolStripButtonBar(CommandBarManager.ActionCommand.Validate);
                        }
                        break;

                    case CommandBarManager.ActionCommand.Calculate:
                        result = CalculateButtonClick();
                        if (result)
                        {
                            BaseStatus = FormStatus.View;
                            SetToolStripButtonBar(CommandBarManager.ActionCommand.Calculate);
                        }
                        break;

                    case CommandBarManager.ActionCommand.Lock:
                        result = LockButtonClick();
                        if (result)
                        {
                            BaseStatus = FormStatus.View;
                            SetToolStripButtonBar(CommandBarManager.ActionCommand.Lock);
                        }
                        break;

                    case CommandBarManager.ActionCommand.SLock:
                        result = SLockButtonClick();
                        if (result)
                        {
                            BaseStatus = FormStatus.View;
                            SetToolStripButtonBar(CommandBarManager.ActionCommand.SLock);
                        }
                        break;

                    case CommandBarManager.ActionCommand.UnLock:
                        result = UnLockButtonClick();
                        if (result)
                        {
                            BaseStatus = FormStatus.View;
                            SetToolStripButtonBar(CommandBarManager.ActionCommand.UnLock);
                        }
                        break;

                    case CommandBarManager.ActionCommand.ExportExcel:
                        BaseStatus = FormStatus.View;
                        result = ExportExcelButtonClick();
                        break;

                    case CommandBarManager.ActionCommand.ImportExcel:
                        BaseStatus = FormStatus.Add;
                        result = ImportExcelButtonClick();
                        break;

                    case CommandBarManager.ActionCommand.Find:
                        BaseStatus = FormStatus.View;
                        result = FindButtonClick();
                        break;

                    case CommandBarManager.ActionCommand.ImportAnotherFile:
                        BaseStatus = FormStatus.View;
                        result = ImportAnotherFileButtonClick();
                        break;

                    case CommandBarManager.ActionCommand.Approve:
                        result = ApproveButtonClick();
                        if (result)
                        {
                            BaseStatus = FormStatus.View;
                            SetToolStripButtonBar(CommandBarManager.ActionCommand.Approve);
                        }
                        break;

                    case CommandBarManager.ActionCommand.ApproveFlexible:

                        result = ApproveFlexibleButtonClick();
                        if (result)
                        {
                            BaseStatus = FormStatus.Edit;
                            SetToolStripButtonBar(CommandBarManager.ActionCommand.ApproveFlexible);
                        }
                        break;

                    case CommandBarManager.ActionCommand.NotApprove:
                        result = NotApproveButtonClick();
                        if (result)
                        {
                            BaseStatus = FormStatus.View;
                            SetToolStripButtonBar(CommandBarManager.ActionCommand.NotApprove);
                        }
                        break;

                    case CommandBarManager.ActionCommand.ImportFromSwipeMC:
                        BaseStatus = FormStatus.View;
                        result = ImportFromSwipeMCButtonClick();
                        break;

                    case CommandBarManager.ActionCommand.SelectObject:
                        BaseStatus = FormStatus.View;
                        result = SelectObjectButtonClick();
                        break;

                    case CommandBarManager.ActionCommand.GenShift:
                        BaseStatus = FormStatus.View;
                        result = GenShiftButtonClick();
                        break;
                    case CommandBarManager.ActionCommand.ImportSignManually:
                        BaseStatus = FormStatus.View;
                        result = ImportSignManuallyClick();
                        break;
                    case CommandBarManager.ActionCommand.ExportTemplate:
                        BaseStatus = FormStatus.View;
                        result = ExportTemplateClick();
                        break;
                    case CommandBarManager.ActionCommand.Print:
                        BaseStatus = FormStatus.View;
                        result = PrintButtonClick();
                        break;
                    default:
                        //MessageBox.Show(string.Format(MsgCmdNotRgt, e.Command.Key),
                        //                this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        break;
                }

                IsProcessing = false;
                this.Cursor = Cursors.Default;
                //grbMain.Enabled = true;
            }
            catch (Exception ex)
            {
                IsProcessing = false;
                this.Cursor = Cursors.Default;
                //grbMain.Enabled = true;

                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Log.Instance.WriteExceptionLog(ex, this.Name + "[" + this.Text + "]");
            }
        }
        private void uiCommandBar1_CommandControlValueChanged(object sender, CommandEventArgs e)
        {
            bool result;
            try
            {
                if (IsProcessing)
                {
                    return;
                }
                else
                {
                    IsProcessing = true;
                    this.Cursor = Cursors.WaitCursor;
                    //grbMain.Enabled = false;
                }

                result = CommandValueChanged(e.Command);

                IsProcessing = false;
                this.Cursor = Cursors.Default;
                //grbMain.Enabled = true;
            }
            catch (Exception ex)
            {
                IsProcessing = false;
                this.Cursor = Cursors.Default;
                //grbMain.Enabled = true;

                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Log.Instance.WriteExceptionLog(ex, this.Name + "[" + this.Text + "]");
            }
        }
        protected bool ExportExcelFromGrid(GridEX jgridData, bool isCheckRow)
        {
            GridEXExporter exporter;
            FileStream stream;
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "XML Spreadsheet (*.xml)|*.xml";
            bool isCheck = false;
            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                exporter = new GridEXExporter();
                exporter.GridEX = jgridData;

                if (jgridData.RootTable.Columns.Count > 0)
                    isCheck = jgridData.RootTable.Columns[0].ActAsSelector;

                exporter.ExportMode = isCheck ? ExportMode.CheckedRows : ExportMode.AllRows;
                //exporter.SheetName = "IMPORT_DATA";
                exporter.IncludeExcelProcessingInstruction = true;
                exporter.IncludeFormatStyle = true;
                exporter.IncludeHeaders = true;
                exporter.IncludeChildTables = true;
                exporter.IncludeCollapsedRows = true;
                exporter.IncludeExcelProcessingInstruction = true;

                stream = new FileStream(saveDialog.FileName, FileMode.Create);
                exporter.Export(stream);
                stream.Flush();
                stream.Close();
                stream.Dispose();
                exporter.Dispose();
            }
            return true;
        }
        protected void SetEnableCommand(CommandBarManager.ActionCommand command, bool status)
        {
            if (uiCommandBar1.Commands.Contains(ConvertHelper.ConvertEnumToString<CommandBarManager.ActionCommand>(command)))
            {
                uiCommandBar1.Commands[ConvertHelper.ConvertEnumToString<CommandBarManager.ActionCommand>(command)].Enabled = status ? Janus.Windows.UI.InheritableBoolean.True : Janus.Windows.UI.InheritableBoolean.False;
            }
        }
        protected bool GetEnableCommand(CommandBarManager.ActionCommand command)
        {
            if (uiCommandBar1.Commands.Contains(ConvertHelper.ConvertEnumToString<CommandBarManager.ActionCommand>(command)))
            {
                return uiCommandBar1.Commands[ConvertHelper.ConvertEnumToString<CommandBarManager.ActionCommand>(command)].Enabled == Janus.Windows.UI.InheritableBoolean.True;
            }
            else return false;
        }
        #endregion
        #region Proteted vitual functions
        protected virtual bool FormLoad()
        {
            BaseStatus = FormStatus.View;
            SetStatusInputControl(this);
            SetToolStripButtonBar(CommandBarManager.ActionCommand.Cancel);
            return true;
        }
        protected virtual bool AddNewButtonClick()
        {
            return true;
        }

        protected virtual bool EditButtonClick()
        {
            return true;
        }

        protected virtual bool SaveButtonClick()
        {
            return true;
        }

        protected virtual bool CancelButtonClick()
        {
            return true;
        }

        protected virtual bool DeleteButtonClick()
        {
            return true;
        }

        protected virtual bool ValidateButtonClick()
        {
            return true;
        }

        protected virtual bool CalculateButtonClick()
        {
            return true;
        }

        protected virtual bool LockButtonClick()
        {
            return true;
        }

        protected virtual bool SLockButtonClick()
        {
            return true;
        }

        protected virtual bool UnLockButtonClick()
        {
            return true;
        }

        protected virtual bool ExportExcelButtonClick()
        {
            return true;
        }

        protected virtual bool ImportExcelButtonClick()
        {
            return true;
        }

        protected virtual bool ImportAnotherFileButtonClick()
        {
            return true;
        }

        protected virtual bool FindButtonClick()
        {
            return true;
        }

        protected virtual bool ApproveButtonClick()
        {
            return true;
        }

        protected virtual bool ApproveFlexibleButtonClick()
        {
            return true;
        }

        protected virtual bool NotApproveButtonClick()
        {
            return true;
        }

        protected virtual bool ImportFromSwipeMCButtonClick()
        {
            return true;
        }

        protected virtual bool SelectObjectButtonClick()
        {
            return true;
        }

        protected virtual bool GenShiftButtonClick()
        {
            return true;
        }

        protected virtual bool ImportSignManuallyClick()
        {
            return true;
        }
        protected virtual bool ExportTemplateClick()
        {
            return true;
        }
        protected virtual bool PrintButtonClick()
        {
            return true;
        }
        protected virtual bool CommandValueChanged(UICommand Command)
        {
            return true;
        }

        protected virtual void SetToolStripButtonBar(CommandBarManager.ActionCommand cmd)
        {
            switch (cmd)
            {
                case CommandBarManager.ActionCommand.New:
                case CommandBarManager.ActionCommand.Edit:
                    SetEnabledCommandNew = false;
                    SetEnabledCommandEdit = false;
                    SetEnabledCommandDelete = false;
                    SetEnabledCommandSave = true;
                    SetEnabledCommandCancel = true;
                    break;

                case CommandBarManager.ActionCommand.Save:
                case CommandBarManager.ActionCommand.Cancel:
                    SetEnabledCommandNew = true;
                    SetEnabledCommandEdit = true;
                    SetEnabledCommandDelete = true;
                    SetEnabledCommandSave = false;
                    SetEnabledCommandCancel = false;
                    break;

                case CommandBarManager.ActionCommand.Delete:
                case CommandBarManager.ActionCommand.Exit:
                    break;
                case CommandBarManager.ActionCommand.Approve:
                    SetEnabledCommandApproveFlexible = true;
                    break;
                case CommandBarManager.ActionCommand.ApproveFlexible:
                    SetEnabledCommandApproveFlexible = false;
                    break;
                case CommandBarManager.ActionCommand.NotApprove:
                    SetEnabledCommandApproveFlexible = true;
                    break;
                default:
                    //uiCommandBar1.Enabled = false;
                    break;
            }
        }

        protected virtual bool AsynchronousFunctionWithBGW(BackgroundWorker bgWorker)
        {
            return true;
        }

        protected virtual void RunAfterWorkerCompleted(bool result)
        {
        }

        protected virtual void ContextMenuClickEvent(UICommand cmd)
        {
        }
        #endregion
        #region Proteted funtions

        protected void BindDataFromGridToForm(HiGrid hiGrid, System.Windows.Forms.Control container)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (hiGrid.SelectedItems.Count > 0)
                {
                    if (hiGrid.SelectedItems[0].RowType == RowType.Record)
                    {
                        ValueObjectToControls(hiGrid.SelectedItems[0].GetRow().DataRow, container);
                    }
                }
                else
                {
                    ResetInputControl(container);
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        protected string GetMessageById(string MsgId)
        {
            return SXml.ReadMessageById(Util.Constant.URL_XML_MESSAGE_RESOURCE, MsgId);
        }

        protected void AddBasicCommandBar()
        {
            CreateCommandBar(CommandBarManager.ActionCommand.New,
                CommandBarManager.ActionCommand.Edit,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Save,
                CommandBarManager.ActionCommand.Cancel,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Delete,
                CommandBarManager.ActionCommand.Exit);
        }

        protected UICommand GetButtonBarCommandByKey(string CmdKey)
        {
            if (uiCommandBar1.Commands.Contains(CmdKey))
            {
                return uiCommandBar1.Commands[CmdKey];
            }
            else
            {
                return null;
            }

        }

        protected void CreateTxtInCommandBar(string CmdKey, string title, int width, string defaultvalue)
        {
            CommandBarManager.CreateTxtInCommandBar(hiCommandManager1, uiCommandBar1, CmdKey, title, width);
            TextBox txt = uiCommandBar1.Commands[CmdKey].GetTextBox();
            txt.Text = defaultvalue;
        }

        protected void CreateDropdownInCommandBar(string CmdKey, string title, int width, int dropdownitem)
        {
            CommandBarManager.CreateDropdownInCommandBar(hiCommandManager1, uiCommandBar1, CmdKey, title, width, dropdownitem);
        }

        protected void SetDataSourceToDropdownCommand(string CmdKey, IList dtSource, string displaymember, string valuemember, bool isDropDown)
        {
            UIComboBox Combo = uiCommandBar1.Commands[CmdKey].GetUIComboBox();
            Combo.ComboStyle = Janus.Windows.EditControls.ComboStyle.DropDownList;
            Combo.DisplayMember = displaymember;
            Combo.ValueMember = valuemember;
            Combo.DataSource = dtSource;
        }

        protected void CreateCommandBar(params CommandBarManager.ActionCommand[] commands)
        {
            //hiCommandManager1.Commands.Clear();
            CommandBarManager.CreateCommandBar(hiCommandManager1, uiCommandBar1, commands);
        }

        protected UICommand GetCommandByKey(string CmdKey)
        {
            if (hiCommandManager1.Commands.Contains(CmdKey))
            {
                return hiCommandManager1.Commands[CmdKey];
            }
            else
            {
                return null;
            }
        }

        protected UICommand GetCommandByKey(CommandBarManager.ActionCommand cmd)
        {
            if (hiCommandManager1.Commands.Contains(ConvertHelper.ConvertEnumToString<CommandBarManager.ActionCommand>(cmd)))
            {
                return hiCommandManager1.Commands[ConvertHelper.ConvertEnumToString<CommandBarManager.ActionCommand>(cmd)];
            }
            else
            {
                return null;
            }
        }

        protected void SetUICommandBarVisible(bool status)
        {
            uiCommandBar1.Visible = status;
        }
        
        protected void WriteErrorLog(Exception ex)
        {
            HiStaff.Util.Log.Instance.WriteExceptionLog(ex, this.Text);
        }

        protected void CreateDropdownInCommandBarWithDataList(string CmdKey, string title, int width, string dataList)
        {
            CreateDropdownInCommandBar(CmdKey, title, width, 20);
            var lstSource = ConvertHelper.DataTableToList<SY_DEFAULT_LIST>(HiStaff.iSystem.Data.DATALIST.Tables[dataList]);
            SetDataSourceToDropdownCommand(CmdKey, lstSource, "NAME", "ID", true);
            if (lstSource.Count > 0)
            {
                GetCommandByKey(CmdKey).GetUIComboBox().SelectedIndex = 0;
            }
        }
        #endregion
        #region Protected Function - Status Bar
        protected void SetStatusText(string status, MessageIcon icon)
        {
            barMainBase.Panels[BAR_STATUSTEXT_KEY].Text = status;
            switch (icon)
            {
                case MessageIcon.Info:
                    barMainBase.Panels[BAR_STATUSTEXT_KEY].Image = SystemIcons.Information.ToBitmap();
                    break;
                case MessageIcon.Warning:
                    barMainBase.Panels[BAR_STATUSTEXT_KEY].Image = SystemIcons.Warning.ToBitmap();
                    break;
                case MessageIcon.Error:
                    barMainBase.Panels[BAR_STATUSTEXT_KEY].Image = SystemIcons.Error.ToBitmap();
                    break;
                default:
                    barMainBase.Panels[BAR_STATUSTEXT_KEY].Image = null;
                    break;
            }
        }

        protected void SetProgressBar(int value)
        {
            barMainBase.Panels[BAR_PROGRESSBAR_KEY].ProgressBarValue = value;
        }

        protected void ExcuteAsynchronous()
        {
            //Application.DoEvents()
            if (!bgWorker.IsBusy)
            {
                //grbMain.Enabled = false;
                this.Enabled = false;
                uiCommandBar1.Enabled = false;
                this.UseWaitCursor = true;
                bgWorker.RunWorkerAsync();
            }
        }

        protected void CancelAsync()
        {
            bgWorker.CancelAsync();
        }

        private void bgWorker_DoWork(System.Object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            e.Result = AsynchronousFunctionWithBGW((BackgroundWorker)sender);
        }

        private void bgWorker_ProgressChanged(System.Object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            try
            {
                ReportProgressValue = e.ProgressPercentage;
                if (e.UserState != null)
                {
                    ReportProgressText = e.UserState.ToString();
                }
                else
                {
                    ReportProgressText = string.Empty;
                }

                SetProgressBar(e.ProgressPercentage);
                SetStatusText(ReportProgressText, MessageIcon.Info);
            }
            catch (Exception ex)
            {
                Log.Instance.WriteExceptionLog(ex, this.Name + "[" + this.Text + "]");
            }
        }

        private void bgWorker_RunWorkerCompleted(System.Object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            this.UseWaitCursor = false;
            uiCommandBar1.Enabled = true;
            //grbMain.Enabled = true;
            this.Enabled = true;

            RunAfterWorkerCompleted((bool)e.Result);
        }
        #endregion
        #region Protected Function for data process
        protected void GetListControlDataField(System.Windows.Forms.Control container)
        {
            if (lstCtrlDataField == null) lstCtrlDataField = new List<System.Windows.Forms.Control>();
            foreach (System.Windows.Forms.Control ctrl in container.Controls)
            {
                switch (ctrl.GetType().Name)
                {
                    case "HiTextBox":
                        if (!string.IsNullOrEmpty(((HiTextBox)ctrl).DataField))
                        {
                            lstCtrlDataField.Add(ctrl);
                        }
                        break;
                    case "HiComboBox":
                        if (!string.IsNullOrEmpty(((HiComboBox)ctrl).DataField))
                        {
                            lstCtrlDataField.Add(ctrl);
                        }
                        break;
                    case "HiCheckBox":
                        if (!string.IsNullOrEmpty(((HiCheckBox)ctrl).DataField))
                        {
                            lstCtrlDataField.Add(ctrl);
                        }
                        break;
                    case "HiDatePicker":
                        if (!string.IsNullOrEmpty(((HiDatePicker)ctrl).DataField))
                        {
                            lstCtrlDataField.Add(ctrl);
                        }
                        break;

                    case "HiLabel":
                        if (!string.IsNullOrEmpty(((HiLabel)ctrl).DataField))
                        {
                            lstCtrlDataField.Add(ctrl);
                        }
                        break;
                    case "HiMaskedTextBox":
                        if (!string.IsNullOrEmpty(((HiMaskedTextBox)ctrl).DataField))
                        {
                            lstCtrlDataField.Add(ctrl);
                        }
                        break;
                    case "HiNumericTextBox":
                        if (!string.IsNullOrEmpty(((HiNumericTextBox)ctrl).DataField))
                        {
                            lstCtrlDataField.Add(ctrl);
                        }
                        break;
                    case "HiTimePicker":
                        if (!string.IsNullOrEmpty(((HiTimePicker)ctrl).DataField))
                        {
                            lstCtrlDataField.Add(ctrl);
                        }
                        break;
                    case "HiValueListUpDown":
                        if (!string.IsNullOrEmpty(((HiValueListUpDown)ctrl).DataField))
                        {
                            lstCtrlDataField.Add(ctrl);
                        }
                        break;
                    case "HiIntegerUpDown":
                        if (!string.IsNullOrEmpty(((HiIntegerUpDown)ctrl).DataField))
                        {
                            lstCtrlDataField.Add(ctrl);
                        }
                        break;
                    case "HiImageBox":
                        if (!string.IsNullOrEmpty(((HiImageBox)ctrl).DataField))
                        {
                            lstCtrlDataField.Add(ctrl);
                        }
                        break;
                    case "HiComboOrganization":
                        if (!string.IsNullOrEmpty(((HiComboOrganization)ctrl).DataField))
                        {
                            lstCtrlDataField.Add(ctrl);
                        }
                        break;
                    case "HiEmployeeSelection":
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldID))
                        {
                            lstCtrlDataField.Add(ctrl);
                        }
                        break;
                    case "HiSalarySelection":
                        if (!string.IsNullOrEmpty(((HiSalarySelection)ctrl).DataField))
                        {
                            lstCtrlDataField.Add(ctrl);
                        }
                        break;
                    case "UIPanel":
                    case "UIPanelGroup":
                    case "UIPanelInnerContainer":
                    case "HiTab":
                    case "UITabPage":
                    case "HiGroupBox":
                        GetListControlDataField(ctrl);
                        break;

                    default:
                        break;
                }
            }
        }
        protected virtual void ValueControlsToObject(Object parameter, System.Windows.Forms.Control container)
        {
            if (parameter == null) return;
            PropertyDescriptorCollection infos = TypeDescriptor.GetProperties(parameter.GetType());
            object value = null;
            string dataField = string.Empty;

            var chk = 0;
            var tmp = 0;
            foreach (System.Windows.Forms.Control ctrl in container.Controls)
            {
                chk = 0;
                switch (ctrl.GetType().Name)
                {
                    case "HiTextBox":
                        value = ((HiTextBox)ctrl).Text;
                        dataField = ((HiTextBox)ctrl).DataField;
                        break;
                    case "HiComboBox":
                        value = ((HiComboBox)ctrl).SelectedValue;
                        dataField = ((HiComboBox)ctrl).DataField;
                        break;
                    case "HiCheckBox":
                        value = ((HiCheckBox)ctrl).Value;
                        dataField = ((HiCheckBox)ctrl).DataField;
                        break;
                    case "HiDatePicker":
                        value = ((HiDatePicker)ctrl).Value;
                        dataField = ((HiDatePicker)ctrl).DataField;
                        break;
                    case "HiLabel":
                        value = ((HiLabel)ctrl).Text;
                        dataField = ((HiLabel)ctrl).DataField;
                        break;
                    case "HiMaskedTextBox":
                        value = ((HiMaskedTextBox)ctrl).Text;
                        dataField = ((HiMaskedTextBox)ctrl).DataField;
                        break;
                    case "HiNumericTextBox":
                        value = ((HiNumericTextBox)ctrl).Value;
                        dataField = ((HiNumericTextBox)ctrl).DataField;
                        break;
                    case "HiTimePicker":
                        value = ((HiTimePicker)ctrl).Value;
                        dataField = ((HiTimePicker)ctrl).DataField;
                        break;
                    case "HiValueListUpDown":
                        value = ((HiValueListUpDown)ctrl).Value;
                        dataField = ((HiValueListUpDown)ctrl).DataField;
                        break;
                    case "HiIntegerUpDown":
                        value = ((HiIntegerUpDown)ctrl).Value;
                        dataField = ((HiIntegerUpDown)ctrl).DataField;
                        break;
                    case "HiComboOrganization":
                        if (((HiComboOrganization)ctrl).SelectedNode != null)
                        {
                            value = ((HiComboOrganization)ctrl).SelectedNode.Name;
                            dataField = ((HiComboOrganization)ctrl).DataField;
                        }
                        break;
                    case "HiTreeOrganization":
                        if (((HiTreeOrganization)ctrl).SelectedNode != null)
                        {
                            value = ((HiTreeOrganization)ctrl).SelectedNode.Name;
                            dataField = ((HiTreeOrganization)ctrl).DataField;
                        }
                        break;
                    case "HiImageBox":
                        value = ((HiImageBox)ctrl).ImageData;
                        dataField = ((HiImageBox)ctrl).DataField;
                        break;
                    case "HiEmployeeSelection":
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldID))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldCode))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldFirstName))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldLastName))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldOrganization))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldOrgID))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldPosition))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldPosID))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldInsCenterID))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldInsCenterName))
                        {
                            chk++;
                        }
                        if (chk != 0)
                        {
                            tmp = 0;
                            foreach (PropertyDescriptor info in infos)
                            {
                                value = null;
                                dataField = null;
                                if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldID)
                                    && info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldID.ToUpper())
                                {
                                    dataField = ((HiEmployeeSelection)ctrl).DataFieldID;
                                    value = ((HiEmployeeSelection)ctrl).SelectedID;
                                    tmp++;
                                }
                                else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldCode)
                                    && info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldCode.ToUpper())
                                {
                                    dataField = ((HiEmployeeSelection)ctrl).DataFieldCode;
                                    value = ((HiEmployeeSelection)ctrl).SelectedCode;
                                    tmp++;
                                }
                                else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldFirstName)
                                    && info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldFirstName.ToUpper())
                                {
                                    dataField = ((HiEmployeeSelection)ctrl).DataFieldFirstName;
                                    value = ((HiEmployeeSelection)ctrl).SelectedFirstName;
                                    tmp++;
                                }
                                else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldLastName)
                                    && info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldLastName.ToUpper())
                                {
                                    dataField = ((HiEmployeeSelection)ctrl).DataFieldLastName;
                                    value = ((HiEmployeeSelection)ctrl).SelectedLastName;
                                    tmp++;
                                }
                                else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldOrganization)
                                    && info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldOrganization.ToUpper())
                                {
                                    dataField = ((HiEmployeeSelection)ctrl).DataFieldOrganization;
                                    value = ((HiEmployeeSelection)ctrl).SelectedOrganization;
                                    tmp++;
                                }
                                else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldOrgID)
                                    && info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldOrgID.ToUpper())
                                {
                                    dataField = ((HiEmployeeSelection)ctrl).DataFieldOrgID;
                                    value = ((HiEmployeeSelection)ctrl).SelectedOrgID;
                                    tmp++;
                                }
                                else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldPosition)
                                    && info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldPosition.ToUpper())
                                {
                                    dataField = ((HiEmployeeSelection)ctrl).DataFieldPosition;
                                    value = ((HiEmployeeSelection)ctrl).SelectedPosition;
                                    tmp++;
                                }
                                else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldPosID)
                                    && info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldPosID.ToUpper())
                                {
                                    dataField = ((HiEmployeeSelection)ctrl).DataFieldPosID;
                                    value = ((HiEmployeeSelection)ctrl).SelectedPosID;
                                    tmp++;
                                }
                                else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldInsCenterID)
                                    && info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldInsCenterID.ToUpper())
                                {
                                    dataField = ((HiEmployeeSelection)ctrl).DataFieldInsCenterID;
                                    value = ((HiEmployeeSelection)ctrl).SelectedInsCenterID;
                                    tmp++;
                                }
                                else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldInsCenterName)
                                    && info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldInsCenterName.ToUpper())
                                {
                                    dataField = ((HiEmployeeSelection)ctrl).DataFieldInsCenterName;
                                    value = ((HiEmployeeSelection)ctrl).SelectedInsCenterName;
                                    tmp++;
                                }

                                if (!string.IsNullOrEmpty(dataField) && info.Name.ToUpper() == dataField.ToUpper())
                                {
                                    TypeConverter typeConverter = info.Converter;
                                    if (value != null)
                                    {
                                        object obj = value;
                                        if (typeConverter != null)
                                        {
                                            if (value.GetType().Name == "Byte[]")
                                            {
                                                obj = value;
                                            }
                                            else
                                                obj = typeConverter.ConvertFromString(value.ToString());
                                        }
                                        info.SetValue(parameter, obj);
                                    }
                                }
                                if (tmp == chk)
                                    break;
                            }
                        }
                        //    continue;
                        //value = ((HiStaff.Domain.HU.EMPLOYEE_SELECTION)((HiEmployeeSelection)ctrl).SelectedItem).ID;
                        //dataField = ((HiEmployeeSelection)ctrl).DataFieldID;
                        value = null;
                        dataField = null;
                        break;
                    case "HiSalarySelection":
                        if (((HiSalarySelection)ctrl).SalarySelected != null)
                        {
                            value = ((HiSalarySelection)ctrl).SalarySelected.ID;
                            dataField = ((HiSalarySelection)ctrl).DataField;
                        }
                        break;
                    case "UIPanel":
                    case "UIPanelGroup":
                    case "UIPanelInnerContainer":
                    case "HiTab":
                    case "UITabPage":
                    case "HiGroupBox":
                        ValueControlsToObject(parameter, ctrl);
                        break;
                    default:
                        value = null;
                        dataField = string.Empty;
                        break;
                }
                if (string.IsNullOrEmpty(dataField))
                    continue;
                foreach (PropertyDescriptor info in infos)
                {
                    if (info.Name.ToUpper() == dataField.ToUpper())
                    {
                        TypeConverter typeConverter = info.Converter;
                        if (value != null)
                        {
                            object obj = value;
                            if (typeConverter != null)
                            {
                                if (value.GetType().Name == "Byte[]")
                                {
                                    obj = value;
                                }
                                else
                                    obj = typeConverter.ConvertFromString(value.ToString());
                            }
                            info.SetValue(parameter, obj);
                        }
                        break;
                    }
                }
            }
        }
        protected virtual void ValueObjectToControls(Object parameter, System.Windows.Forms.Control container)
        {
            if (parameter == null) return;
            PropertyDescriptorCollection infos = TypeDescriptor.GetProperties(parameter.GetType());
            bool isBind;
            int chk;
            int tmp;
            foreach (System.Windows.Forms.Control ctrl in container.Controls)
            {
                isBind = false;
                chk = 0;
                switch (ctrl.GetType().Name)
                {
                    case "HiTextBox":
                        if (string.IsNullOrEmpty(((HiTextBox)ctrl).DataField))
                        {
                            continue;
                        }
                        break;
                    case "HiComboBox":
                        if (string.IsNullOrEmpty(((HiComboBox)ctrl).DataField))
                        {
                            continue;
                        }
                        break;
                    case "HiCheckBox":
                        if (string.IsNullOrEmpty(((HiCheckBox)ctrl).DataField))
                        {
                            continue;
                        }
                        break;
                    case "HiDatePicker":
                        if (string.IsNullOrEmpty(((HiDatePicker)ctrl).DataField))
                        {
                            continue;
                        }
                        break;
                    case "HiLabel":
                        if (string.IsNullOrEmpty(((HiLabel)ctrl).DataField))
                        {
                            continue;
                        }
                        break;
                    case "HiMaskedTextBox":
                        if (string.IsNullOrEmpty(((HiMaskedTextBox)ctrl).DataField))
                        {
                            continue;
                        }
                        break;
                    case "HiNumericTextBox":
                        if (string.IsNullOrEmpty(((HiNumericTextBox)ctrl).DataField))
                        {
                            continue;
                        }
                        break;
                    case "HiTimePicker":
                        if (string.IsNullOrEmpty(((HiTimePicker)ctrl).DataField))
                        {
                            continue;
                        }
                        break;
                    case "HiValueListUpDown":
                        if (string.IsNullOrEmpty(((HiValueListUpDown)ctrl).DataField))
                        {
                            continue;
                        }
                        break;
                    case "HiIntegerUpDown":
                        if (string.IsNullOrEmpty(((HiIntegerUpDown)ctrl).DataField))
                        {
                            continue;
                        }
                        break;
                    case "HiImageBox":
                        if (string.IsNullOrEmpty(((HiImageBox)ctrl).DataField))
                        {
                            continue;
                        }
                        break;
                    case "HiComboOrganization":
                        if (string.IsNullOrEmpty(((HiComboOrganization)ctrl).DataField))
                        {
                            continue;
                        }
                        break;
                    case "HiEmployeeSelection":
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldID))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldCode))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldFirstName))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldLastName))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldOrganization))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldOrgID))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldPosition))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldPosID))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldInsCenterID))
                        {
                            chk++;
                        }
                        if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldInsCenterName))
                        {
                            chk++;
                        }
                        if (chk == 0)
                            continue;
                        break;
                    case "HiSalarySelection":
                        if (string.IsNullOrEmpty(((HiSalarySelection)ctrl).DataField) ||
                            string.IsNullOrEmpty(((HiSalarySelection)ctrl).DataFieldEmployeeID))
                        {
                            continue;
                        }
                        break;
                    case "UIPanel":
                    case "UIPanelGroup":
                    case "UIPanelInnerContainer":
                    case "HiTab":
                    case "UITabPage":
                    case "HiGroupBox":
                        ValueObjectToControls(parameter, ctrl);
                        continue;
                    default:
                        continue;
                }
                tmp = 0;
                foreach (PropertyDescriptor info in infos)
                {
                    switch (ctrl.GetType().Name)
                    {
                        case "HiTextBox":
                            if (info.Name.ToUpper() == ((HiTextBox)ctrl).DataField.ToUpper())
                            {
                                ((HiTextBox)ctrl).Text = ConvertHelper.ToString(info.GetValue(parameter));
                                isBind = true;
                            }
                            break;
                        case "HiComboBox":
                            if (info.Name.ToUpper() == ((HiComboBox)ctrl).DataField.ToUpper())
                            {
                                ((HiComboBox)ctrl).SelectedValue = info.GetValue(parameter);
                                isBind = true;
                            }
                            break;
                        case "HiCheckBox":
                            if (info.Name.ToUpper() == ((HiCheckBox)ctrl).DataField.ToUpper())
                            {
                                ((HiCheckBox)ctrl).ReadOnly = false;
                                ((HiCheckBox)ctrl).Value = ConvertHelper.ToDecimal(info.GetValue(parameter));
                                ((HiCheckBox)ctrl).ReadOnly = true;
                                isBind = true;
                            }
                            break;
                        case "HiDatePicker":
                            if (info.Name.ToUpper() == ((HiDatePicker)ctrl).DataField.ToUpper())
                            {
                                ((HiDatePicker)ctrl).Value = ConvertHelper.ToDateTime(info.GetValue(parameter));
                                isBind = true;
                            }
                            break;
                        case "HiLabel":
                            if (info.Name.ToUpper() == ((HiLabel)ctrl).DataField.ToUpper())
                            {
                                ((HiLabel)ctrl).Text = ConvertHelper.ToString(info.GetValue(parameter));
                                isBind = true;
                            }
                            break;
                        case "HiMaskedTextBox":
                            if (info.Name.ToUpper() == ((HiMaskedTextBox)ctrl).DataField.ToUpper())
                            {
                                ((HiMaskedTextBox)ctrl).Text = ConvertHelper.ToString(info.GetValue(parameter));
                                isBind = true;
                            }
                            break;
                        case "HiNumericTextBox":
                            if (info.Name.ToUpper() == ((HiNumericTextBox)ctrl).DataField.ToUpper())
                            {
                                ((HiNumericTextBox)ctrl).Value = ConvertHelper.ToDecimal(info.GetValue(parameter));
                                isBind = true;
                            }
                            break;
                        case "HiTimePicker":
                            if (info.Name.ToUpper() == ((HiTimePicker)ctrl).DataField.ToUpper())
                            {
                                ((HiTimePicker)ctrl).Value = ConvertHelper.ToDateTime(info.GetValue(parameter));
                                isBind = true;
                            }
                            break;
                        case "HiValueListUpDown":
                            if (info.Name.ToUpper() == ((HiValueListUpDown)ctrl).DataField.ToUpper())
                            {
                                ((HiValueListUpDown)ctrl).Value = info.GetValue(parameter);
                                isBind = true;
                            }
                            break;
                        case "HiIntegerUpDown":
                            if (info.Name.ToUpper() == ((HiIntegerUpDown)ctrl).DataField.ToUpper())
                            {
                                ((HiIntegerUpDown)ctrl).Value = ConvertHelper.ToInt(info.GetValue(parameter));
                                isBind = true;
                            }
                            break;
                        case "HiImageBox":
                            if (info.Name.ToUpper() == ((HiImageBox)ctrl).DataField.ToUpper())
                            {
                                ((HiImageBox)ctrl).ImageData = info.GetValue(parameter);
                                isBind = true;
                            }
                            break;
                        case "HiComboOrganization":
                            if (info.Name.ToUpper() == ((HiComboOrganization)ctrl).DataField.ToUpper())
                            {
                                ((HiComboOrganization)ctrl).ReadOnly = false;
                                ((HiComboOrganization)ctrl).SelectedNode = ((HiComboOrganization)ctrl).FindNodeByName(ConvertHelper.ToString(info.GetValue(parameter)));
                                ((HiComboOrganization)ctrl).ReadOnly = true;
                                isBind = true;
                            }
                            break;
                        case "HiEmployeeSelection":
                            if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldID) &&
                                info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldID.ToUpper())
                            {
                                tmp++;
                                ((HiEmployeeSelection)ctrl).SelectedID = ConvertHelper.ToDecimal(info.GetValue(parameter));
                            }
                            else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldCode) &&
                                info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldCode.ToUpper())
                            {
                                tmp++;
                                ((HiEmployeeSelection)ctrl).SelectedCode = ConvertHelper.ToString(info.GetValue(parameter));
                            }
                            else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldFirstName) &&
                                info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldFirstName.ToUpper())
                            {
                                tmp++;
                                ((HiEmployeeSelection)ctrl).SelectedFirstName = ConvertHelper.ToString(info.GetValue(parameter));
                            }
                            else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldLastName) &&
                                info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldLastName.ToUpper())
                            {
                                tmp++;
                                ((HiEmployeeSelection)ctrl).SelectedLastName = ConvertHelper.ToString(info.GetValue(parameter));
                            }
                            else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldOrganization) &&
                                info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldOrganization.ToUpper())
                            {
                                tmp++;
                                ((HiEmployeeSelection)ctrl).SelectedOrganization = ConvertHelper.ToString(info.GetValue(parameter));
                            }
                            else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldOrgID) &&
                                info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldOrgID.ToUpper())
                            {
                                tmp++;
                                ((HiEmployeeSelection)ctrl).SelectedOrgID = ConvertHelper.ToDecimal(info.GetValue(parameter));
                            }
                            else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldPosID) &&
                                info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldPosID.ToUpper())
                            {
                                tmp++;
                                ((HiEmployeeSelection)ctrl).SelectedPosID = ConvertHelper.ToDecimal(info.GetValue(parameter));
                            }
                            else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldPosition) &&
                                info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldPosition.ToUpper())
                            {
                                tmp++;
                                ((HiEmployeeSelection)ctrl).SelectedPosition = ConvertHelper.ToString(info.GetValue(parameter));
                            }
                            else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldInsCenterID) &&
                                info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldInsCenterID.ToUpper())
                            {
                                tmp++;
                                ((HiEmployeeSelection)ctrl).SelectedInsCenterID = ConvertHelper.ToString(info.GetValue(parameter));
                            }
                            else if (!string.IsNullOrEmpty(((HiEmployeeSelection)ctrl).DataFieldInsCenterName) &&
                                info.Name.ToUpper() == ((HiEmployeeSelection)ctrl).DataFieldInsCenterName.ToUpper())
                            {
                                tmp++;
                                ((HiEmployeeSelection)ctrl).SelectedInsCenterName = ConvertHelper.ToString(info.GetValue(parameter));
                            }
                            if (tmp == chk)
                            {
                                ((HiEmployeeSelection)ctrl).Refresh();
                                isBind = true;
                            }

                            break;
                        case "HiSalarySelection":
                            if (info.Name.ToUpper() == ((HiSalarySelection)ctrl).DataFieldEmployeeID.ToUpper())
                            {
                                ((HiSalarySelection)ctrl).EmployeeID = ConvertHelper.ToDecimal(info.GetValue(parameter));
                            }
                            if (info.Name.ToUpper() == ((HiSalarySelection)ctrl).DataField.ToUpper())
                            {
                                ((HiSalarySelection)ctrl).SelectedItem = ((HiSalarySelection)ctrl).GetItemByID(ConvertHelper.ToDecimal(info.GetValue(parameter)));
                                isBind = true;
                            }
                            break;
                        case "UIPanel":
                        case "UIPanelGroup":
                        case "UIPanelInnerContainer":
                        case "HiTab":
                        case "UITabPage":
                        case "HiGroupBox":
                            ValueObjectToControls(parameter, ctrl);
                            isBind = true;
                            break;

                        default:
                            isBind = true;
                            break;
                    }
                    if (isBind)
                        break;
                }
            }
        }
        protected virtual bool CheckValidation(System.Windows.Forms.Control container)
        {
            foreach (System.Windows.Forms.Control ctrl in container.Controls)
            {
                switch (ctrl.GetType().Name)
                {
                    case "HiTextBox":
                        if (((HiTextBox)ctrl).IsRequire && string.IsNullOrEmpty(((HiTextBox)ctrl).Text.Trim()))
                        {
                            ctrl.Focus();
                            MessageBox.Show(GetMessageById("MSG_FIELD_NOTBLANK"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return false;
                        }
                        break;
                    case "HiComboBox":
                        if (((HiComboBox)ctrl).IsRequire && ((HiComboBox)ctrl).SelectedValue == null)
                        {
                            ctrl.Focus();
                            MessageBox.Show(GetMessageById("MSG_FIELD_NOTBLANK"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return false;
                        }
                        break;
                    case "HiCheckBox":
                        break;
                    case "HiDatePicker":
                        if (((HiDatePicker)ctrl).IsRequire && !((HiDatePicker)ctrl).Value.HasValue)
                        {
                            ctrl.Focus();
                            MessageBox.Show(GetMessageById("MSG_FIELD_NOTBLANK"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return false;
                        }
                        break;
                    case "HiLabel":
                        break;
                    case "HiMaskedTextBox":
                        if (((HiMaskedTextBox)ctrl).IsRequire && !((HiMaskedTextBox)ctrl).IsTextValid() && string.IsNullOrEmpty(((HiMaskedTextBox)ctrl).Text))
                        {
                            ctrl.Focus();
                            MessageBox.Show(GetMessageById("MSG_FIELD_NOTBLANK"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return false;
                        }
                        break;
                    case "HiNumericTextBox":
                        if (((HiNumericTextBox)ctrl).IsRequire && ((HiNumericTextBox)ctrl).Value == null)
                        {
                            ctrl.Focus();
                            MessageBox.Show(GetMessageById("MSG_FIELD_NOTBLANK"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return false;
                        }
                        break;
                    case "HiTimePicker":
                        if (((HiTimePicker)ctrl).IsRequire && !((HiTimePicker)ctrl).Value.HasValue)
                        {
                            ctrl.Focus();
                            MessageBox.Show(GetMessageById("MSG_FIELD_NOTBLANK"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return false;
                        }
                        break;
                    case "HiValueUpDown":
                        if (((HiValueListUpDown)ctrl).IsRequire && ((HiValueListUpDown)ctrl).Value == null)
                        {
                            ctrl.Focus();
                            MessageBox.Show(GetMessageById("MSG_FIELD_NOTBLANK"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return false;
                        }
                        break;
                    case "HiIntegerUpDown":
                        if (((HiIntegerUpDown)ctrl).IsRequire && ((HiIntegerUpDown)ctrl).Value == null)
                        {
                            ctrl.Focus();
                            MessageBox.Show(GetMessageById("MSG_FIELD_NOTBLANK"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return false;
                        }
                        break;
                    case "HiComboOrganization":
                        if (((HiComboOrganization)ctrl).IsRequire && ((HiComboOrganization)ctrl).SelectedNode == null)
                        {
                            ctrl.Focus();
                            MessageBox.Show(GetMessageById("MSG_FIELD_NOTBLANK"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return false;
                        }
                        break;
                    case "HiImageBox":
                        if (((HiImageBox)ctrl).IsRequire && ((HiImageBox)ctrl).ImageData == null)
                        {
                            ctrl.Focus();
                            MessageBox.Show(GetMessageById("MSG_FIELD_NOTBLANK"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return false;
                        }
                        break;
                    case "HiGrid":
                        break;
                    case "HiEmployeeSelection":
                        if (((HiEmployeeSelection)ctrl).IsRequire && (((HiEmployeeSelection)ctrl).EmployeeSelected == null || ((HiEmployeeSelection)ctrl).EmployeeSelected.Count == 0))
                        {
                            ctrl.Focus();
                            MessageBox.Show(GetMessageById("MSG_FIELD_NOTBLANK"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return false;
                        }
                        break;
                    case "HiSalarySelection":
                        if (((HiSalarySelection)ctrl).IsRequire && ((HiSalarySelection)ctrl).SalarySelected == null)
                        {
                            ctrl.Focus();
                            MessageBox.Show(GetMessageById("MSG_FIELD_NOTBLANK"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return false;
                        }
                        break;
                    case "UIPanel":
                    case "UIPanelGroup":
                    case "UIPanelInnerContainer":
                    case "HiTab":
                    case "UITabPage":
                    case "HiGroupBox":
                        if (!CheckValidation(ctrl))
                            return false;
                        break;
                    default:
                        break;
                }
            }
            return true;
        }
        protected virtual void SetStatusInputControl(System.Windows.Forms.Control container)
        {
            foreach (System.Windows.Forms.Control ctrl in container.Controls)
            {
                switch (ctrl.GetType().Name)
                {
                    case "HiTextBox":
                        if (((HiTextBox)ctrl).SearchControl)
                        {
                            ((HiTextBox)ctrl).Enabled = (BaseStatus != FormStatus.Edit &&
                                BaseStatus != FormStatus.Add);
                        }
                        else
                        {
                            ((HiTextBox)ctrl).ReadOnly = (BaseStatus != FormStatus.Add) &&
                                (BaseStatus != FormStatus.Edit || ((HiTextBox)ctrl).ReadOnlyWhenEdit);
                        }
                        break;
                    case "HiComboBox":
                        if (((HiComboBox)ctrl).SearchControl)
                        {
                            ((HiComboBox)ctrl).Enabled = (BaseStatus != FormStatus.Edit &&
                                BaseStatus != FormStatus.Add);
                        }
                        else
                        {
                            ((HiComboBox)ctrl).ReadOnly = (BaseStatus != FormStatus.Add) &&
                                                           (BaseStatus != FormStatus.Edit || ((HiComboBox)ctrl).ReadOnlyWhenEdit);
                        }
                        break;
                    case "HiCheckBox":
                        if (((HiCheckBox)ctrl).SearchControl)
                        {
                            ((HiCheckBox)ctrl).Enabled = (BaseStatus != FormStatus.Edit &&
                                BaseStatus != FormStatus.Add);
                        }
                        else
                        {
                            ((HiCheckBox)ctrl).ReadOnly = (BaseStatus != FormStatus.Add) &&
                                                           (BaseStatus != FormStatus.Edit || ((HiCheckBox)ctrl).ReadOnlyWhenEdit);
                        }
                        break;
                    case "HiDatePicker":
                        if (((HiDatePicker)ctrl).SearchControl)
                        {
                            ((HiDatePicker)ctrl).Enabled = (BaseStatus != FormStatus.Edit &&
                                BaseStatus != FormStatus.Add);
                        }
                        else
                        {
                            ((HiDatePicker)ctrl).ReadOnly = (BaseStatus != FormStatus.Add) &&
                                                           (BaseStatus != FormStatus.Edit || ((HiDatePicker)ctrl).ReadOnlyWhenEdit);
                        }
                        break;
                    case "HiLabel":
                        break;
                    case "HiMaskedTextBox":
                        if (((HiMaskedTextBox)ctrl).SearchControl)
                        {
                            ((HiMaskedTextBox)ctrl).Enabled = (BaseStatus != FormStatus.Edit &&
                                BaseStatus != FormStatus.Add);
                        }
                        else
                        {
                            ((HiMaskedTextBox)ctrl).ReadOnly = (BaseStatus != FormStatus.Add) &&
                                                           (BaseStatus != FormStatus.Edit || !((HiMaskedTextBox)ctrl).ReadOnlyWhenEdit);
                        }
                        break;
                    case "HiNumericTextBox":
                        if (((HiNumericTextBox)ctrl).SearchControl)
                        {
                            ((HiNumericTextBox)ctrl).Enabled = (BaseStatus != FormStatus.Edit &&
                                BaseStatus != FormStatus.Add);
                        }
                        else
                        {
                            ((HiNumericTextBox)ctrl).ReadOnly = (BaseStatus != FormStatus.Add) &&
                                                           (BaseStatus != FormStatus.Edit || ((HiNumericTextBox)ctrl).ReadOnlyWhenEdit);
                        }
                        break;
                    case "HiTimePicker":
                        if (((HiTimePicker)ctrl).SearchControl)
                        {
                            ((HiTimePicker)ctrl).Enabled = (BaseStatus != FormStatus.Edit &&
                                BaseStatus != FormStatus.Add);
                        }
                        else
                        {
                            ((HiTimePicker)ctrl).ReadOnly = (BaseStatus != FormStatus.Add) &&
                                                           (BaseStatus != FormStatus.Edit || ((HiTimePicker)ctrl).ReadOnlyWhenEdit);
                        }
                        break;
                    case "HiValueListUpDown":
                        if (((HiValueListUpDown)ctrl).SearchControl)
                        {
                            ((HiValueListUpDown)ctrl).Enabled = (BaseStatus != FormStatus.Edit &&
                                BaseStatus != FormStatus.Add);
                        }
                        else
                        {
                            ((HiValueListUpDown)ctrl).ReadOnly = (BaseStatus != FormStatus.Add) &&
                                                           (BaseStatus != FormStatus.Edit || ((HiValueListUpDown)ctrl).ReadOnlyWhenEdit);
                        }
                        break;
                    case "HiIntegerUpDown":
                        if (((HiIntegerUpDown)ctrl).SearchControl)
                        {
                            ((HiIntegerUpDown)ctrl).Enabled = (BaseStatus != FormStatus.Edit &&
                                BaseStatus != FormStatus.Add);
                        }
                        else
                        {
                            ((HiIntegerUpDown)ctrl).ReadOnly = (BaseStatus != FormStatus.Add) &&
                                                           (BaseStatus != FormStatus.Edit || ((HiIntegerUpDown)ctrl).ReadOnlyWhenEdit);
                        }
                        break;
                    case "HiTreeOrganization":
                        if (((HiTreeOrganization)ctrl).SearchControl)
                        {
                            ((HiTreeOrganization)ctrl).Enabled = (BaseStatus != FormStatus.Edit &&
                                                           BaseStatus != FormStatus.Add);
                        }
                        break;
                    case "HiComboOrganization":
                        ((HiComboOrganization)ctrl).ReadOnly = (BaseStatus != FormStatus.Add) &&
                                                       (BaseStatus != FormStatus.Edit || ((HiComboOrganization)ctrl).ReadOnlyWhenEdit);
                        break;
                    case "HiImageBox":
                        ((HiImageBox)ctrl).ReadOnly = (BaseStatus != FormStatus.Add) &&
                                                       (BaseStatus != FormStatus.Edit || ((HiImageBox)ctrl).ReadOnlyWhenEdit);
                        break;
                    case "HiGrid":
                        if (((HiGrid)ctrl).MainGrid)
                        {
                            ((HiGrid)ctrl).Enabled = (BaseStatus != FormStatus.Add &&
                                BaseStatus != FormStatus.Edit);
                        }
                        else if (BaseStatus == FormStatus.Edit || BaseStatus == FormStatus.Add)
                        {
                            ((HiGrid)ctrl).AllowEdit = InheritableBoolean.True;
                        }
                        else
                        {
                            ((HiGrid)ctrl).AllowEdit = InheritableBoolean.False;
                        }
                        break;
                    case "HiEmployeeSelection":
                        ((HiEmployeeSelection)ctrl).ReadOnly = (BaseStatus != FormStatus.Add) &&
                                                                (BaseStatus != FormStatus.Edit || ((HiEmployeeSelection)ctrl).ReadOnlyWhenEdit);
                        break;
                    case "HiSalarySelection":
                        ((HiSalarySelection)ctrl).ReadOnly = (BaseStatus != FormStatus.Add) &&
                                                                (BaseStatus != FormStatus.Edit || ((HiSalarySelection)ctrl).ReadOnlyWhenEdit);
                        break;
                    case "UIPanel":
                    case "UIPanelGroup":
                    case "UIPanelInnerContainer":
                    case "HiTab":
                    case "UITabPage":
                    case "HiGroupBox":
                        SetStatusInputControl(ctrl);
                        break;
                    default:
                        break;
                }
            }
        }
        protected virtual void ResetInputControl(System.Windows.Forms.Control container)
        {
            foreach (System.Windows.Forms.Control ctrl in container.Controls)
            {
                switch (ctrl.GetType().Name)
                {
                    case "HiTextBox":
                        if (!((HiTextBox)ctrl).SearchControl)
                        {
                            ((HiTextBox)ctrl).Text = null;
                        }
                        break;
                    case "HiComboBox":
                        if (!((HiComboBox)ctrl).SearchControl)
                        {
                            ((HiComboBox)ctrl).SelectedItem = null;
                        }
                        break;
                    case "HiCheckBox":
                        if (!((HiCheckBox)ctrl).SearchControl)
                        {
                            ((HiCheckBox)ctrl).ReadOnly = false;
                            ((HiCheckBox)ctrl).Checked = false;
                            ((HiCheckBox)ctrl).ReadOnly = true;
                        }
                        break;
                    case "HiDatePicker":
                        if (!((HiDatePicker)ctrl).SearchControl)
                        {
                            ((HiDatePicker)ctrl).Value = null;
                        }
                        break;
                    case "HiLabel":
                        if (!string.IsNullOrEmpty(((HiLabel)ctrl).DataField))
                        {
                            ((HiLabel)ctrl).Text = null;
                        }
                        break;
                    case "HiMaskedTextBox":
                        if (!((HiMaskedTextBox)ctrl).SearchControl)
                        {
                            ((HiMaskedTextBox)ctrl).Text = null;
                        }
                        break;
                    case "HiNumericTextBox":
                        if (!((HiNumericTextBox)ctrl).SearchControl)
                        {
                            ((HiNumericTextBox)ctrl).Value = null;
                        }
                        break;
                    case "HiTimePicker":
                        if (!((HiTimePicker)ctrl).SearchControl)
                        {
                            ((HiTimePicker)ctrl).Value = null;
                        }
                        break;
                    case "HiValueListUpDown":
                        if (!((HiValueListUpDown)ctrl).SearchControl)
                        {
                            ((HiValueListUpDown)ctrl).Value = null;
                        }
                        break;
                    case "HiIntegerUpDown":
                        if (!((HiIntegerUpDown)ctrl).SearchControl)
                        {
                            ((HiIntegerUpDown)ctrl).Value = 0;
                        }
                        break;
                    case "HiComboOrganization":
                        if (((HiComboOrganization)ctrl).Nodes.Count > 0)
                        {
                            ((HiComboOrganization)ctrl).ReadOnly = false;
                            ((HiComboOrganization)ctrl).SelectedNode = null;
                            ((HiComboOrganization)ctrl).ReadOnly = true;
                        }
                        break;
                    case "HiImageBox":
                        ((HiImageBox)ctrl).ImageData = null;
                        break;
                    case "HiGrid":
                        //if (!((HiGrid)ctrl).MainGrid)
                        //{
                            //((HiGrid)ctrl).DataSource = null;
                            //((HiGrid)ctrl).Refresh();
                        //}
                        break;
                    case "HiEmployeeSelection":
                        ((HiEmployeeSelection)ctrl).EmployeeSelected = null;
                        break;
                    case "HiSalarySelection":
                        ((HiSalarySelection)ctrl).EmployeeID = null;
                        break;
                    case "UIPanel":
                    case "UIPanelGroup":
                    case "UIPanelInnerContainer":
                    case "HiTab":
                    case "UITabPage":
                    case "HiGroupBox":
                        ResetInputControl(ctrl);
                        break;
                    default:
                        break;
                }
            }
        }
        #endregion
        #region Jerry add
        protected void CreateContextMenuGrid(bool hasSeparator, string parentkey, string itemkey, string itemtext)
        {
            CommandBarManager.CreateContextMenuItem(hiCommandManager1, uiContextMenu1, hasSeparator, parentkey, itemkey, itemtext);
        }
        protected UICommand GetContextCommandByKey(string CtxKey)
        {
            if (uiContextMenu1.Commands.Contains(CtxKey))
            {
                return uiContextMenu1.Commands[CtxKey];
            }
            else
            {
                return null;
            }
        }
        #endregion
    }
}